import os,django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "shareAPI2020TB.settings")# project_name 项目名称
django.setup()
import logging
import re

import requests

server_logger = logging.getLogger("server")
import time
from django.contrib.admin.utils import unquote
from requests.adapters import HTTPAdapter
from tbitem import models
#解析stick链接
def get_redirect_url(jump_url):

    # server_logger.error(f"step2-----'进入get_redirect_url函数'")
    headers = {
        "upgrade-insecure-requests": "1",
        "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36",
        "sec-fetch-mode": "navigate",
        "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
        "sec-fetch-site": "none",
        "accept-encoding": "gzip, deflate, br",
        "accept-language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,ja-JP;q=0.6,ja;q=0.5",
    }
    s = requests.Session()
    s.mount('http://', HTTPAdapter(max_retries=3))  # 重试3次
    s.mount('https://', HTTPAdapter(max_retries=3))
    res = ''
    try:
        res = s.get(url=jump_url, headers=headers, timeout=3).text
    except requests.exceptions.RequestException as e:
        logging.error('淘宝goods_id重试失败', e)
    # 提取原始链接请求响应体中要302重定向的链接
    if re.findall("real_jump_address = \'(.*)\'", res):
        new_url = re.findall("real_jump_address = \'(.*)\'", res)[0].replace('amp;', '')
        # 必须加上referer
        headers['referer'] = jump_url
        # 这里需要禁止重定向
        resp = requests.get(url=new_url, headers=headers, allow_redirects=False)
        redirects_url = resp.headers.get('location')
        redirects_url = unquote(unquote(redirects_url))
        goods_id = re.findall(r".*?id=(.*?)&", redirects_url)[0]
        # print(goods_id)
        # print(resp.text)
        return goods_id
    else:
        return ''
def readTxt():
    listData = []
    # 2.按行读取
    for line in open('1234.txt', errors='ignore',encoding='utf-8'):
        # 清洗空格后如果不为空就下一步处理
        if line.strip() != '':
            # print(f"本行内容为{line}")
            # 每行的数据都需要清洗空格后进行下一步处理
            abc = line.strip()
            title = abc.split('@@@', 2)[0]
            # print(title)
            tburl = abc.split('@@@', 2)[1]
            # print(tburl)

            if len(title) >4 and len(tburl) >4:
                listData.append([f"https://s.click.taobao.com/t?{tburl}",title])
        else:
            print("本行为空")
    return listData

def setp0():
    for item in readTxt():  #按行读取链接和标题


        title = item[1]
        queryNum = models.DIYTitle.objects.filter(title=title).first()  # 是否已存在
        if queryNum:  # 存在就不再操作他了
            print('已经存在')
            continue
        else:
            itemid = get_redirect_url(item[0])  # 跳转拿到tbid
            print("休息50妙")
            time.sleep(50)  # 休息50秒
            if len(itemid) <= 10:  # 判断id是否可用
                print(f'{itemid}不是商品id，忽略')
                continue
            else:
                models.DIYTitle.objects.create(
                    tbid=itemid,
                    title=title,
                    status=1,
                )

if __name__ == '__main__':
    setp0()
